<script setup lang="ts">
import VDialog from "@/components/VDialog/VDialog.vue";
import { computed, reactive, ref } from "vue";
import {
  AddConversationRecordCommand,
  ConversationRecordPageResponse,
  UpdateConversationRecordCommand,
  addConversationRecordApi,
  updateConversationRecordApi
} from "@/api/bot/conversation-record";
import { ElMessage, FormInstance, FormRules } from "element-plus";

interface Props {
  type: "add" | "update";
  modelValue: boolean;
  row?: ConversationRecordPageResponse;
}

const props = defineProps<Props>();
const emits = defineEmits<{
  (e: "update:modelValue", v: boolean): void;
  (e: "success"): void;
}>();

const visible = computed({
  get: () => props.modelValue,
  set(v) {
    emits("update:modelValue", v);
  }
});

const formData = reactive<
  AddConversationRecordCommand | UpdateConversationRecordCommand
>({
  id: 0,
  spoken: "",
  receivedName: "",
  groupName: "",
  roomType: "",
  answer: "",
  tokenUse: 0
});

const rules: FormRules = {
  spoken: [{ required: true, message: "发言内容不能为空" }],
  answer: [{ required: true, message: "回答内容不能为空" }]
};

const formRef = ref<FormInstance>();

function handleOpened() {
  if (props.row) {
    Object.assign(formData, props.row);
  } else {
    formRef.value?.resetFields();
  }
}

const loading = ref(false);

async function handleConfirm() {
  try {
    loading.value = true;
    if (props.type === "add") {
      await addConversationRecordApi(formData);
    } else if (props.type === "update") {
      await updateConversationRecordApi(
        formData as UpdateConversationRecordCommand
      );
    }
    ElMessage.info("提交成功");
    visible.value = false;
    emits("success");
  } catch (e) {
    console.error(e);
    ElMessage.error((e as Error)?.message || "提交失败");
  } finally {
    loading.value = false;
  }
}
</script>

<template>
  <v-dialog
    show-full-screen
    :fixed-body-height="false"
    use-body-scrolling
    :title="type === 'add' ? '新增会话记录' : '更新会话记录'"
    v-model="visible"
    :loading="loading"
    @confirm="handleConfirm"
    @cancel="visible = false"
    @opened="handleOpened"
  >
    <el-form :model="formData" label-width="120px" :rules="rules" ref="formRef">
      <el-form-item prop="spoken" label="发言内容" required>
        <el-input v-model="formData.spoken" type="textarea" rows="3" />
      </el-form-item>
      <el-form-item prop="receivedName" label="接收者">
        <el-input v-model="formData.receivedName" />
      </el-form-item>
      <el-form-item prop="groupName" label="群组名称">
        <el-input v-model="formData.groupName" />
      </el-form-item>
      <el-form-item prop="roomType" label="房间类型">
        <el-input v-model="formData.roomType" />
      </el-form-item>
      <el-form-item prop="answer" label="回答内容" required>
        <el-input v-model="formData.answer" type="textarea" rows="6" />
      </el-form-item>
      <el-form-item prop="tokenUse" label="token消耗">
        <el-input-number v-model="formData.tokenUse" :min="0" />
      </el-form-item>
    </el-form>
  </v-dialog>
</template>
